
import { groupData, ModifyGroupEnum } from "./groupData";
import { GCtrl } from "./GCtrl";
import { friendData, AgentType } from "./friendData";
import { GroupVerifyData } from "./GroupVerifyData";


export class GroupDetailData extends cc.Component {
    private constructor() {
        super();
        this.onEvent();
        // this.getAllGroupData();
    };
    private static m_pInstance: GroupDetailData = null;
    public static getInstance() {
        if (!this.m_pInstance) {
            this.m_pInstance = new GroupDetailData();
        }
        return this.m_pInstance;
    }
    private accountInfo: any = {};
    private groupDataArr: any = {};
    private groupIdArr: number[] = [];//存储群ID
    private myClubArr: number[] = [];//我所在的俱乐部ID数组
    private myOwnClubArr: number[] = [];//我是部长的俱乐部ID数组
    private myGroupArr: number[] = [];//我所在的群ID数组

    private groupIdArrList: any = {};
    private groupIdArrIndexList: any = {};

    //temp
    public isCreateGroup: boolean = false;

    private onEvent() {
        GCtrl.eventIndex['ServerNtc_GroupDetailData'] = cc.vv.eventCustom.on("ServerNtc", this.serverNtc.bind(this));
        GCtrl.eventIndex['logout_gdd'] = cc.vv.eventCustom.on('logout', this.logout.bind(this), this);
    }
    private offEvent() {
        GCtrl.eventIndex['logout_gdd'] && (cc.vv.eventCustom.off('logout', GCtrl.eventIndex['logout_gdd']), delete GCtrl.eventIndex['logout_gdd']);
        GCtrl.eventIndex['ServerNtc_GroupDetailData'] && (cc.vv.eventCustom.off("ServerNtc", GCtrl.eventIndex['ServerNtc_GroupDetailData']), delete GCtrl.eventIndex['ServerNtc_GroupDetailData']);
    }
    //---------------------------------------
    //数据获取
    //---------------------------------------

    private getAllGroupData() {
        let groupInfoArr = null;
        let b = false;
        if (GCtrl.dataConfig.SelfData == null || GCtrl.dataConfig.SelfData == void 0) {
            let key = GCtrl.dataConfig.GROUP_DATA_KEY;
            this.groupIdArr = JSON.parse(cc.sys.localStorage.getItem(this.accountInfo.phone_num + '_' + key)) || [];
            for (let i in this.groupIdArr) {
                let groupId = this.groupIdArr[i];
                let groupInfo = this.getLocalStorage(groupId);
                if (groupInfo) {
                    this.groupDataArr[groupInfo.Id] = groupInfo;
                }
            }
        }
        else {
            this.groupDataArr = {};
            b = true;
            groupInfoArr = GCtrl.dataConfig.SelfData.group_list;
            if (!groupInfoArr) return false;
            for (let i in groupInfoArr) {
                let groupInfo = groupInfoArr[i];
                this.saveGroupData({ info: groupInfo });
            }
        }
        this.saveMyClubAMyGroup();
        if (b) {
            return this.contrastData();
        }
        else return b;
    }
    private requestData(msgType: string, msg: any, callback = null, ...args) {
        cc.vv.GLUtils.sendRequest(msgType, msg, function (error, data) {
            if (data.errorcode) {
                cc.vv.GLUtils.showTips(data.errordes);
            } else {
                callback && callback(data, ...args);
            }
        }.bind(this));
    }
    private searchGroup(Id: number, data: any = null) {
        let groupDataList = data || this.groupDataArr;
        let groupInfo: groupData = groupDataList[Id];
        return groupInfo;
    }
    private getLocalStorage(Id: number) {
        let key = GCtrl.dataConfig.GROUP_DATA_KEY;
        let groupDataJson = cc.sys.localStorage.getItem(this.accountInfo.phone_num + '_' + key + '_' + Id);
        if (!groupDataJson) return;
        let groupInfo: groupData = JSON.parse(groupDataJson);
        return groupInfo;
    }

    //---------------------------------------
    //数据操作
    //---------------------------------------

    private serverNtc(data: any) {
        if (!data) return;
        if (!data.length || data.length == 0) return;
        else {
            let self = this;
            let ntc = data[0];
            let new_grp_arr = ntc.new_grp;
            let del_grp_arr = ntc.del_grp;
            let mdf_grp_arr = ntc.mdf_grp;
            if (new_grp_arr && new_grp_arr.length) {
                //数据和通知一起过来
                for (let i in new_grp_arr) {
                    this.addNewGroupData({ info: new_grp_arr[i] }, 'addNewGroupSuccess');
                }
            }
            if (del_grp_arr && del_grp_arr.length) {
                for (let i in del_grp_arr) {
                    let groupId = del_grp_arr[i];
                    let groupInfo = this.groupDataArr[groupId];
                    if (groupInfo) {
                        delete this.groupDataArr[groupId];
                        this.removeGroupId(groupId);
                        cc.vv.eventCustom.emit('removeGroupSuccess', groupId);
                        cc.vv.chatMgr.clearOneSessionMessage(groupId, true);
                        this.removeLocalStorage(groupId);
                    }
                }
            }
            if (mdf_grp_arr && mdf_grp_arr.length) {
                for (let i in mdf_grp_arr) {
                    this.updateGroupData(mdf_grp_arr[i]);
                }
            }
        }
    }
    private removeGroupId(groupId: number) {
        let index = this.myGroupArr.indexOf(groupId);
        index != -1 && (this.myGroupArr.splice(index, 1));
        index = this.myClubArr.indexOf(groupId);
        index != -1 && (this.myClubArr.splice(index, 1));
        index = this.groupIdArr.indexOf(groupId);
        index != -1 && (this.myClubArr.splice(index, 1));
        index = this.myOwnClubArr.indexOf(groupId);
        index != -1 && (this.myClubArr.splice(index, 1));
    }
    private logout() {
        this.groupDataArr = {};
        this.groupIdArrIndexList = {};
        this.groupIdArrList = {};
        this.groupIdArr = [];
        this.myClubArr = [];
        this.myOwnClubArr = [];
        GroupDetailData.m_pInstance = null;
        this.offEvent();
    }
    private saveGroupData(data: any) {
        if (data && data.info) {
            let _groupData = data.info;
            let groupInfo = groupData.s2cData(_groupData);
            this.groupDataArr[_groupData.gid] = groupInfo;
            if (groupInfo.isClub) {
                let type = cc.vv.session.getType();
                if (type == AgentType.user || type == AgentType.playerWithoutClub) {
                    cc.vv.session.setType(AgentType.playerWithClub);
                    type = cc.vv.session.getType();
                    cc.vv.eventCustom.emit('updateIdentity', type);
                }
            }
            cc.vv.eventCustom.emit("GroupDataGetSuccess", this.groupDataArr[_groupData.gid]);
            this.updateVerifyInfo(groupInfo);
        }
    }
    private updateVerifyInfo(groupInfo: groupData) {
        if (!groupInfo) return false;
        GroupVerifyData.getInstance().newVerifyData(groupInfo.groupRequestInfoArr);
        return true;
    }
    private addNewGroupData(data: any, ...args) {
        if (data) {
            let eventName = args[0];
            let _groupData = data.info;
            let groupInfo = groupData.s2cData(_groupData);
            this.groupDataArr[_groupData.gid] = groupInfo;
            if (groupInfo.isClub) {
                let myId = cc.vv.session.getRid();
                let type = cc.vv.session.getType();
                if (type == AgentType.user || type == AgentType.playerWithoutClub) {
                    cc.vv.session.setType(AgentType.playerWithClub);
                    type = cc.vv.session.getType();
                    cc.vv.eventCustom.emit('updateIdentity', type);
                }
                this.myClubArr.push(groupInfo.Id);
                myId == groupInfo.groupLeaderId && (this.myOwnClubArr.push(groupInfo.Id));
                //进入俱乐部
                cc.vv.session.setCurClub(groupInfo);
                cc.vv.eventCustom.emit("updateClub");
            }
            cc.vv.eventCustom.emit(eventName, this.groupDataArr[_groupData.gid]);
            this.setLocalStorage(groupInfo.Id, groupInfo);
            this.isCreateGroup && (
                GCtrl.eventCustom.emit('openChat', {
                    friendinfo: groupInfo,
                    from: 0
                }),
                GCtrl.eventCustom.emit('updateNewMsgNum',groupInfo.Id, 0),
                this.isCreateGroup = false);
        }
    }
    private setLocalStorage(Id: number, groupInfo: groupData = null) {
        groupInfo || (groupInfo = this.searchGroup(Id));
        if (!groupInfo) return false;
        let key = GCtrl.dataConfig.GROUP_DATA_KEY;
        cc.sys.localStorage.setItem(this.accountInfo.phone_num + '_' + key + '_' + Id, JSON.stringify(groupInfo));
        return true;
    }
    private removeLocalStorage(groupId: number) {
        let key = GCtrl.dataConfig.GROUP_DATA_KEY;
        let localData = cc.sys.localStorage.getItem(this.accountInfo.phone_num + '_' + key + '_' + groupId);
        if (!localData) return false;
        cc.sys.localStorage.removeItem(key + '_' + groupId);
        return true;
    }
    private saveStorage() {
        let key = GCtrl.dataConfig.GROUP_DATA_KEY;
        for (let i in this.groupDataArr) {
            let groupInfo = this.groupDataArr[i];
            groupInfo && cc.sys.localStorage.setItem(this.accountInfo.phone_num + '_' + key + '_' + groupInfo.Id, JSON.stringify(groupInfo));
            this.groupIdArr.indexOf(Number(i)) == -1 && this.groupIdArr.push(Number(i));
        }
        cc.sys.localStorage.setItem(this.accountInfo.phone_num + '_' + key, JSON.stringify(this.groupIdArr));
        return true;
    }
    //对比本地缓存和服务器数据的差异(以服务器为准)
    private contrastData() {
        let key = GCtrl.dataConfig.GROUP_DATA_KEY;
        for (let i in this.groupDataArr) {
            let groupInfo_server = this.groupDataArr[i];
            let groupId = groupInfo_server.Id;
            let groupInfo_local = JSON.parse(cc.sys.localStorage.getItem(this.accountInfo.phone_num + '_' + key + '_' + groupId));
            groupInfo_local || (groupInfo_local = {});
            for (let j in groupInfo_server) {
                groupInfo_local[j] = groupData.contrastData(j, groupInfo_local[j], groupInfo_server[j]);
            }
            this.groupDataArr[i] = groupInfo_local;
        }
        return this.saveStorage();
    }
    private saveMyClubAMyGroup() {
        this.myClubArr = [];
        this.myOwnClubArr = [];
        this.myGroupArr = [];
        let myId = cc.vv.session.getPlayer().rid;
        let allGroupData = this.groupDataArr;
        for (let i in allGroupData) {
            let groupInfo: groupData = allGroupData[i];
            groupInfo.isClub ?
                (this.myClubArr.push(groupInfo.Id),
                    groupInfo.groupLeaderId == myId && this.myOwnClubArr.push(groupInfo.Id)) :
                this.myGroupArr.push(groupInfo.Id);
        }
        if (!this.myClubArr.length) {
            return false;
        }
        else
            return true;
    }
    private updateGroupData(modifyGroupInfo: any) {
        if (!modifyGroupInfo) return false;
        let _groupData: groupData = this.groupDataArr[modifyGroupInfo.gid];
        if (!_groupData) return false;
        let type = modifyGroupInfo.type;
        switch (type) {
            case ModifyGroupEnum.NAME: {
                _groupData.name = modifyGroupInfo.name;
                cc.vv.eventCustom.emit('updateGroupDataSuccess', _groupData.Id, { type, data: modifyGroupInfo.name });
            } break;
            case ModifyGroupEnum.NOTE: {
                _groupData.notice = modifyGroupInfo.note;
                cc.vv.eventCustom.emit('updateGroupDataSuccess', _groupData.Id, { type, data: modifyGroupInfo.note });
            } break;
            case ModifyGroupEnum.OWNER: {
                let leaderId = _groupData.groupLeaderId;
                _groupData.groupLeaderId = modifyGroupInfo.owner;
                _groupData.groupMemberIdArr.push(leaderId);
                let index = _groupData.groupMemberIdArr.indexOf(modifyGroupInfo.owner);
                if (index != -1) _groupData.groupMemberIdArr.splice(index, 1);
                cc.vv.eventCustom.emit('updateGroupDataSuccess', _groupData.Id, { type, data: modifyGroupInfo.owner });
            } break;
            case ModifyGroupEnum.FORBID: {
                _groupData.isSilenceAll = modifyGroupInfo.forbid;
                cc.vv.eventCustom.emit('updateGroupDataSuccess', _groupData.Id, { type, data: modifyGroupInfo.forbid });
            } break;
            case ModifyGroupEnum.PROTECT: {
                _groupData.isProtectModel = modifyGroupInfo.protect;
                cc.vv.eventCustom.emit('updateGroupDataSuccess', _groupData.Id, { type, data: modifyGroupInfo.protect });
            } break;
            case ModifyGroupEnum.ADMIN_ADD: {
                let memberId = modifyGroupInfo.admin_rid;
                _groupData.groupControllerIdArr.push(memberId);
                let index = _groupData.groupMemberIdArr.indexOf(memberId);
                if (index != -1) _groupData.groupMemberIdArr.splice(index, 1);
                cc.vv.eventCustom.emit('updateGroupDataSuccess', _groupData.Id, { type, data: modifyGroupInfo.admin_rid });
            } break;
            case ModifyGroupEnum.ADMIN_DEL: {
                let memberId = modifyGroupInfo.admin_rid;
                _groupData.groupMemberIdArr.push(memberId);
                let index = _groupData.groupControllerIdArr.indexOf(memberId);
                if (index != -1) _groupData.groupControllerIdArr.splice(index, 1);
                cc.vv.eventCustom.emit('updateGroupDataSuccess', _groupData.Id, { type, data: modifyGroupInfo.admin_rid });
            } break;
            case ModifyGroupEnum.SAYMSG_ADD: {
                let memberId = modifyGroupInfo.saymsg_rid;
                _groupData.groupSilenceIdArr.push(memberId);
                cc.vv.eventCustom.emit('updateGroupDataSuccess', _groupData.Id, { type, data: modifyGroupInfo.saymsg_rid });
            } break;
            case ModifyGroupEnum.SAYMSG_DEL: {
                let memberId = modifyGroupInfo.saymsg_rid;
                let index = _groupData.groupSilenceIdArr.indexOf(memberId);
                if (index != -1) _groupData.groupSilenceIdArr.splice(index, 1);
                cc.vv.eventCustom.emit('updateGroupDataSuccess', _groupData.Id, { type, data: modifyGroupInfo.saymsg_rid });
            } break;
            case ModifyGroupEnum.REQUEST: {
                GroupVerifyData.getInstance().newVerifyData([modifyGroupInfo.request]);
                cc.vv.eventCustom.emit('updateGroupDataSuccess', _groupData.Id, { type, data: modifyGroupInfo.request });
            } break;
            case ModifyGroupEnum.MEMBER_ADD: {
                let userData = friendData.s2cData(modifyGroupInfo.member);
                _groupData.groupMemberIdArr.push(userData.Id);
                _groupData.memberNum = _groupData.groupMemberIdArr.length + _groupData.groupControllerIdArr.length + 1;
                _groupData.groupAllMemberDataArr[userData.Id] = userData;
                if (_groupData.diedMemberData[userData.Id]) delete _groupData.diedMemberData[userData.Id];
                cc.vv.eventCustom.emit('updateGroupDataSuccess', _groupData.Id, { type, data: userData });
            } break;
            case ModifyGroupEnum.MEMBER_DEL: {
                let userId = modifyGroupInfo.member.rid;
                let index = _groupData.groupMemberIdArr.indexOf(userId);
                let userData = _groupData.groupAllMemberDataArr[userId];
                if (index != -1) {
                    _groupData.groupMemberIdArr.splice(index, 1);
                    _groupData.memberNum = _groupData.groupMemberIdArr.length + _groupData.groupControllerIdArr.length + 1;
                    if (userData) {
                        _groupData.diedMemberData[userData.Id] = userData;
                        delete _groupData.groupAllMemberDataArr[userId];
                    };
                }
                cc.vv.eventCustom.emit('updateGroupDataSuccess', _groupData.Id, { type, data: userData });
            } break;
        }
        this.setLocalStorage(_groupData.Id, _groupData);
        return true;
    }
    //-------------------
    init() {
        this.accountInfo = GCtrl.GLUtils.getAccountInfo();
        this.getAllGroupData();
    }
    //---------------------------------------
    //数据获取
    //---------------------------------------

    /**
     * 获取所有群的数据 {groupId: groupData}
     * @param eventName 接收数据的事件名
     */
    public getGroupList(eventName: string = '') {
        cc.vv.eventCustom.emit(eventName, this.groupDataArr);
        return this.groupDataArr;
    }
    /**
     * 获取指定ID的群数据
     * @param groupId 群ID
     * @param eventName 接收数据的事件名
     */
    public getGroupData(groupId: number, eventName: string = ''): groupData {
        let _groupData: groupData = this.searchGroup(groupId);
        cc.vv.eventCustom.emit(eventName, _groupData);
        return _groupData;
    }
    /**
     * 获取符合筛选条件的群数据
     * @param condition 筛选条件
     */
    public shaiXuanData(condition: string = '') {
        if (condition == '') return this.groupDataArr;
        let _hArr = {};
        let { ...tempArr } = this.groupDataArr;
        for (let i in tempArr) {
            let _groupData: groupData = tempArr[i];
            let Id = _groupData.Id + '';
            let name = _groupData.name;
            if (name.indexOf(condition) != -1 || Id.indexOf(condition) != -1) {
                _hArr[i] = _groupData;
            }
        }
        return _hArr;
    }
    /**
     * 获取指定群中符合筛选条件的群成员数据
     * @param groupId 群ID
     * @param condition 筛选条件
     */
    public filterGroupMember(groupId: number, condition: string = '') {
        let groupInfo: groupData = this.groupDataArr[groupId];
        if (!groupInfo) return false;
        if (condition == '') return groupInfo.groupAllMemberDataArr;
        let _hArr = {};
        let { ...tempArr } = groupInfo.groupAllMemberDataArr;
        for (let i in tempArr) {
            let _userData: friendData = tempArr[i];
            let Id = _userData.Id + '';
            let name = _userData.name;
            if (name.indexOf(condition) == -1 && Id.indexOf(condition) == -1) {
            }
            else {
                _hArr[i] = _userData;
            }
        }
        return _hArr;
    }
    /**
     * 获取'我'是部长的俱乐部
     * @param userId 用户ID
     */
    public getOwnClub(userId: number) {
        for (let i in this.groupDataArr) {
            let groupInfo: groupData = this.groupDataArr[i];
            if (groupInfo.isClub && groupInfo.groupLeaderId == userId) {
                return groupInfo;
            }
        }
    }
    /**
     * 获取我的群ID列表
     */
    public getMyGroupIdList() {
        return this.myGroupArr;
    }
    /**
     * 获取我的群个数
     */
    public getMyAllGroupCount() {
        return this.myGroupArr.length + this.myClubArr.length;
    }
    /**
     * 获取我的俱乐部ID列表
     */
    public getMyClubIdList() {
        return this.myClubArr;
    }
    /**
     * 获取我是部长的俱乐部ID列表
     */
    public getMyOwnClubIdList() {
        return this.myOwnClubArr;
    }
    /**
     * 是否在任一俱乐部中
     */
    public isInAnyClub() {
        return !!this.myClubArr.length;
    }
    /**
     * 是否是俱乐部部长
     */
    public isClubOwner() {
        return !!this.myOwnClubArr.length;
    }
    /**
     * 获取指定群未读消息数
     * @param groupId 群ID
     */
    public getUnreadMsgNum(groupId: number) {
        let groupInfo = this.searchGroup(groupId);
        if (!groupInfo) return 0;
        return groupInfo.unreadMsgNum;
    }

    /**
     * 获取指定群数据
     * @param Id 群ID
     */
    public getlocaluserData(groupId: number) {
        let userData = this.getLocalStorage(groupId)
        if (!userData) return null;
        return userData;
    }
    /**
     * 获取指定群最新已读消息ID
     * @param Id 群ID
     */
    public getReadedMsgId(groupId: number) {
        let userData = this.getLocalStorage(groupId)
        if (!userData) return 0;
        return userData.readedMsgId;
    }
    /**
     * 判断一个用户是否在指定群中
     * @param groupId 群ID
     * @param userId 用户ID
     */
    public isInGroup(groupId: number, userId: number) {
        if (groupId == null || groupId == void 0) return false;
        let groupInfo = this.searchGroup(groupId);
        if (!groupInfo) return false;
        return !!groupInfo.groupAllMemberDataArr[userId];
    }
    //-------------------------------------------
    //数据操作
    //-------------------------------------------

    /**
     * 添加管理员
     * @param managerIdArr 新增管理员ID数组
     * @param groupId 群ID
     */
    public addManagerToGroup(managerIdArr: number[], groupId: number) {
        if (!managerIdArr) return false;
        let _groupData: groupData = this.groupDataArr[groupId];
        if (!_groupData) return false;
        // for (let i in managerIdArr) {
        //     let n = Number(i) + 1;
        //     let b = 0.2;
        //     let dl = b * n;
        let r = _groupData.isClub ? 'ClubAdminMgr' : 'GroupAdminMgr';
        let msg: any = { users_id: managerIdArr, operation: true };
        _groupData.isClub ? msg.club_id = groupId : msg.group_id = groupId;
        this.requestData(r, msg);
        // }
        return true;
    }
    /**
     * 删除管理员
     * @param managerIdArr 移除管理员ID数组
     * @param groupId 群ID
     */
    public deleteManagerFromGroup(managerIdArr: number[], groupId: number) {
        if (!managerIdArr) return false;
        let _groupData: groupData = this.groupDataArr[groupId];
        if (!_groupData) return false;
        // for (let i in managerIdArr) {
        //     let id = managerIdArr[i];
        //     let index = _groupData.groupControllerIdArr.indexOf(id);
        //     if (index != -1) {
        //         let n = Number(i) + 1;
        //         let b = 0.2;
        //         let dl = b * n;
        let r = _groupData.isClub ? 'ClubAdminMgr' : 'GroupAdminMgr';
        let msg: any = { users_id: managerIdArr, operation: false };
        _groupData.isClub ? msg.club_id = groupId : msg.group_id = groupId;
        this.requestData(r, msg);
        // }
        // }
        this.setLocalStorage(groupId, _groupData);
        return true;
    }
    /**
     * 添加群成员
     * @param memberIdArr 新增群成员ID数组
     * @param groupId 群ID
     */
    public addMemberToGroup(memberIdArr: number[], groupId: number) {
        if (!memberIdArr) return false;
        let _groupData: groupData = this.groupDataArr[groupId];
        if (!_groupData) return false;
        //存入数据
        // for (let i in memberIdArr) {
        //     let n = Number(i) + 1;
        //     let b = 0.2;
        //     let dl = b * n;
        let r = _groupData.isClub ? 'ClubUserMgr' : 'GroupUserMgr';
        let msg: any = { users_id: memberIdArr, operation: true };
        _groupData.isClub ? msg.club_id = groupId : msg.group_id = groupId;
        this.requestData(r, msg);
        // }
        return true;
    }
    /**
     * 删除群成员
     * @param memberIdArr 移出群成员ID数组
     * @param groupId 群ID
     */
    public deleteMemberFromGroup(memberIdArr: number[], groupId: number) {
        if (!memberIdArr) return false;
        let _groupData: groupData = this.groupDataArr[groupId];
        if (!_groupData) return false;
        // for (let i in memberIdArr) {
        //     let id = memberIdArr[i];
        //     let index = _groupData.groupMemberIdArr.indexOf(id);
        //     if (index != -1) {
        //         let n = Number(i) + 1;
        //         let b = 0.2;
        //         let dl = b * n;
        let r = _groupData.isClub ? 'ClubUserMgr' : 'GroupUserMgr';
        let msg: any = { users_id: memberIdArr, operation: false };
        _groupData.isClub ? msg.club_id = groupId : msg.group_id = groupId;
        this.requestData(r, msg);
        //     }
        // }
        return true;
    }
    /**
     * 移交群主
     * @param memberId 群成员ID
     * @param groupId 群ID
     */
    public changeLeader(memberId: number, groupId: number) {
        let _groupData: groupData = this.groupDataArr[groupId];
        if (!_groupData) return false;
        let index = _groupData.groupControllerIdArr.indexOf(memberId);
        if (index == -1) {
            index = _groupData.groupMemberIdArr.indexOf(memberId);
        }
        if (index == -1) return false;
        this.requestData('GroupAbdicate', { group_id: groupId, user_id: memberId },);
        return true;
    }
    /**
     * 群保护模式开关
     * @param groupId 群ID 
     * @param model (on/off)
     */
    public changeProtectModel(groupId: number, model: string) {
        if (model != 'on' && model != 'off') return false;
        let _groupData: groupData = this.groupDataArr[groupId];
        if (!_groupData) return false;
        let b: boolean = null;
        switch (model) {
            case GCtrl.dataConfig.PROTECT_MODEL.ON: b = true; break;
            case GCtrl.dataConfig.PROTECT_MODEL.OFF: b = false; break;
        }
        if (b != null && _groupData.isProtectModel != b) {
            this.requestData('GroupProtect', { group_id: _groupData.Id, protect: b });
            return true;
        }
        return false;
    }
    /**
     * 修改群名
     * @param groupId 群ID
     * @param newName 新群名
     */
    public changeGroupName(groupId: number, newName: string) {
        let groupInfo: groupData = this.groupDataArr[groupId];
        if (!groupInfo) return false;
        let self = this;
        let r = groupInfo.isClub ? 'ClubRename' : 'GroupRename';
        let msg: any = { comments: newName };
        groupInfo.isClub ? msg.club_id = groupId : msg.group_id = groupId;
        this.requestData(r, msg);
        return true;
    }
    /**
     * 添加禁言成员
     * @param groupId 群ID
     * @param Id 被禁言成员ID
     */
    public addSilencerToGroup(groupId: number, usersId: number[]) {
        if (usersId == null || usersId == void 0) return false;
        let _groupData: groupData = this.groupDataArr[groupId];
        if (!_groupData) return false;
        this.requestData('GroupSaymsg', { group_id: groupId, users_id: usersId, saymsg: true });
        return true;
    }
    /**
     * 移除禁言成员
     * @param groupId 群ID
     * @param Id 成员ID
     */
    public removeSilencerFromGroup(groupId: number, usersId: number[]) {
        if (usersId == null || usersId == void 0) return false;
        let _groupData: groupData = this.groupDataArr[groupId];
        if (!_groupData) return false;
        this.requestData('GroupSaymsg', { group_id: groupId, users_id: usersId, saymsg: false });
        return true;
    }
    /**
     * 全体禁言
     * @param groupId 
     * @param b true 禁言, false 解禁
     */
    public silenceAll(groupId: number, b: boolean) {
        let _groupData: groupData = this.groupDataArr[groupId];
        if (!_groupData) return false;
        if (_groupData.isSilenceAll != b) {
            this.requestData('GroupForbidChat', { group_id: groupId, forbid: b });
            return true;
        }
        return false;
    }
    /**
     * 设置免打扰模式
     * @param groupId 群ID 
     * @param b true开启 false关闭
     */
    public setDontBother(groupId: number, b: boolean) {
        let _groupData: groupData = this.groupDataArr[groupId];
        if (!_groupData) return false;
        if (_groupData.isDontBother != b) {
            _groupData.isDontBother = b;
            cc.vv.eventCustom.emit('setGroupDontBotherSuccess', groupId, b);
            this.setLocalStorage(groupId, _groupData);
            return true;
        }
    }
    /**
     * 设置置顶聊天模式
     * @param groupId 群ID
     * @param b true开启 false关闭
     */
    public setTop(groupId: number, b: boolean) {
        let _groupData: groupData = this.groupDataArr[groupId];
        if (!_groupData) return false;
        if (_groupData.isTop != b) {
            _groupData.isTop = b;
            cc.vv.eventCustom.emit('setGroupTopSuccess', groupId, b);
            this.setLocalStorage(groupId, _groupData);
            return true;
        }
    }
    /**
     * 设置显示昵称
     * @param groupId 群ID
     * @param b true开启 false关闭
     */
    public setShowName(groupId: number, b: boolean) {
        let _groupData: groupData = this.groupDataArr[groupId];
        if (!_groupData) return false;
        if (_groupData.isShowName != b) {
            _groupData.isShowName = b;
            cc.vv.eventCustom.emit('setShowNameSuccess', groupId, b);
            this.setLocalStorage(groupId, _groupData);
            return true;
        }
    }
    /**
     * 清除所有群组的聊天记录
     */
    public ClearAllSession() {
        for (let i in this.groupDataArr) {
            let groupInfo = this.groupDataArr[i];
            cc.vv.chatMgr.clearOneSessionMessage(groupInfo.Id, true);
        }
    }
    /**
     * 设置置顶群未读消息数
     * @param groupId 群ID
     * @param num 消息数
     */
    public setUnreadMsgNum(groupId: number, num: number) {
        let groupInfo = this.searchGroup(groupId);
        if (!groupInfo || num == null || num == void 0) return false;
        groupInfo.unreadMsgNum = num;
        this.setLocalStorage(groupId, groupInfo);
        return true;
    }
    /**
     * 设置置顶群最新已读消息ID
     * @param Id 群ID
     * @param index 消息ID
     */
    public setReadedMsgId(groupId: number, index: number) {
        let groupInfo = this.searchGroup(groupId);
        if (!groupInfo || index == null || index == void 0) return false;
        groupInfo.readedMsgId = index;
        cc.vv.chatMgr.setNewMsgNum(groupId, 0);
        cc.vv.eventCustom.emit('updateNewMsgNum', groupId, 0);
        this.setLocalStorage(groupId, groupInfo);
        return true;
    }
    /**
     * 解散群
     * @param groupId 群ID
     */
    public dissolveGroup(groupId: number) {
        let groupInfo = this.searchGroup(groupId);
        if (!groupInfo) return false;
        this.requestData('GroupDissolve', { group_id: groupId });
        return true;
    }
    /**
     * 退出 群/俱乐部
     * @param groupId 群ID
     */
    public quitGroup(groupId: number) {
        let groupInfo = this.searchGroup(groupId);
        if (!groupInfo) return false;
        groupInfo.isClub ? this.requestData('QuitClub', { club_id: groupId }) : this.requestData('QuitGroup', { club_id: groupId });
        return true;
    }
    /**
     * 获取成员容量
     * @param groupId 
     */
    public getMemberCapacity(groupId: number) {
        let groupInfo = this.searchGroup(groupId);
        if (!groupInfo) return -1;
        let extendedNum = groupInfo.memberExtendedNum;
        let basicNum: number = JSON.parse(GCtrl.dataConfig.SelfData.config).club_member_max;
        let curMaxCapacity = basicNum + extendedNum;
        return curMaxCapacity;
    }
    public getTableCapacity(groupId: number) {
        let groupInfo = this.searchGroup(groupId);
        if (!groupInfo) return false;

    }
    /**
     * 添加已扩容人数
     * @param groupId 
     * @param extendNum 
     */
    public addMemberExtendNum(groupId: number, extendNum: number) {
        if (typeof extendNum != "number") return false;
        let groupInfo = this.searchGroup(groupId);
        if (!groupInfo) return false;
        groupInfo.memberExtendedNum += extendNum;
        return true;
    }
    public addTableExtendNum() {

    }
}